package com.yunhui.common.evn;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service
public class GlobalEnvironment {
	private static final Logger LOG = LoggerFactory.getLogger(GlobalEnvironment.class);

	@Value("${pageSize}")
	private String pageSize;

	@Value("${file.local.rootDir}")
	private String rootDir;

	@Value("${environment}")
	private String environment;

	/**
	 * codeSeq功能阶段: 阶段1 - 从PG读取codeValue; 阶段2 -
	 * 从PG读取codeValue,并与redis中的比对，一定次数相同后，才能进入下一阶段; 阶段3 - 从redis读取code;
	 */
	@Value("${jingxu.codeSequence.functionPhase:1}")
	private int codeSeqFuncPhase;

	private static GlobalEnvironment instance;

	public static synchronized GlobalEnvironment getInstance() {
		instance = new GlobalEnvironment();
		return instance;
	}

	public String resolveString(String s) {
		if (s == null) {
			return "";
		}
		if (s.equals("${pageSize}")) {// 简单实现
			return pageSize;
		}
		return s;
	}

	public Integer resolveInteger(String mightbePlaceHolder) {
		if (mightbePlaceHolder == null) {
			return null;
		}
		String s = resolveString(mightbePlaceHolder);
		if ((s == null) || (s.trim().equals(""))) {
			return null;
		}
		try {
			return Integer.valueOf(s);
		} catch (Exception e) {
		}
		return null;
	}

	public String get(String key) {
		return "";
	}

	public String getEnvironment() {
		return environment;
	}

	public void setEnvironment(String environment) {
		LOG.info("设置系统运行环境为：" + environment);
		this.environment = environment;
	}

	public int getCodeSeqFuncPhase() {
		return codeSeqFuncPhase;
	}

	public void setCodeSeqFuncPhase(int codeSeqFuncPhase) {
		this.codeSeqFuncPhase = codeSeqFuncPhase;
	}
}
